package com.tjmc.company.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjmc.company.api.enums.CompanyMessageEnum;
import com.tjmc.company.api.entity.CompanyMessageTemplateEntity;
import com.tjmc.company.service.CompanyMessageTemplateService;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.log.annotation.SysLog;
import com.tjmc.recruit.common.security.annotation.HasPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息模板内容表
 *
 * @author recruit
 * @date 2024-11-18 15:12:37
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/companyMessageTemplate")
@Tag(description = "companyMessageTemplate", name = "消息模板内容表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CompanyMessageTemplateController {

    private final CompanyMessageTemplateService companyMessageTemplateService;

    /**
     * 分页查询
     *
     * @param page                   分页对象
     * @param companyMessageTemplate 消息模板内容表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R getCompanyMessageTemplatePage(@ParameterObject Page page, @ParameterObject CompanyMessageTemplateEntity companyMessageTemplate) {
        LambdaQueryWrapper<CompanyMessageTemplateEntity> wrapper = Wrappers.lambdaQuery(companyMessageTemplate);
        return R.ok(companyMessageTemplateService.page(page, wrapper));
    }


    /**
     * 通过条件查询消息模板内容表
     *
     * @param companyMessageTemplate 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询", description = "通过条件查询对象")
    @GetMapping("/details")
    public R getDetails(@ParameterObject CompanyMessageTemplateEntity companyMessageTemplate) {
        return R.ok(companyMessageTemplateService.list(Wrappers.query(companyMessageTemplate)));
    }

    /**
     * 新增消息模板内容表
     *
     * @param companyMessageTemplate 消息模板内容表
     * @return R
     */
    @Operation(summary = "新增消息模板内容表", description = "新增消息模板内容表")
    @SysLog("新增消息模板内容表")
    @PostMapping
    public R save(@RequestBody CompanyMessageTemplateEntity companyMessageTemplate) {
        if (companyMessageTemplate.getMessageType().equals(CompanyMessageEnum.GREET)) {
            List<CompanyMessageTemplateEntity> greetList =
                    companyMessageTemplateService.list(new LambdaQueryWrapper<CompanyMessageTemplateEntity>()
                            .eq(CompanyMessageTemplateEntity::getAccount, companyMessageTemplate.getAccount())
                            .eq(CompanyMessageTemplateEntity::getMessageType, CompanyMessageEnum.GREET));
            if (greetList.size() == 5){
                return R.failed("打招呼语最多可设5条");
            }
        }
        return R.ok(companyMessageTemplateService.save(companyMessageTemplate));
    }

    /**
     * 修改消息模板内容表
     *
     * @param companyMessageTemplate 消息模板内容表
     * @return R
     */
    @Operation(summary = "修改消息模板内容表", description = "修改消息模板内容表")
    @SysLog("修改消息模板内容表")
    @PutMapping
    public R updateById(@RequestBody CompanyMessageTemplateEntity companyMessageTemplate) {
        return R.ok(companyMessageTemplateService.updateById(companyMessageTemplate));
    }

    /**
     * 通过id删除消息模板内容表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除消息模板内容表", description = "通过id删除消息模板内容表")
    @SysLog("通过id删除消息模板内容表")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(companyMessageTemplateService.removeBatchByIds(CollUtil.toList(ids)));
    }
}
